﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPPivotViewer.Utility
{
    public static class PV4SPSettings
    {

        public const string cstMaxItem = "PV4SP.MaxItem";
        public const string cst3Layers = "PV4SP.3Layers";
        public const string cstThumbs = "PV4SP.Thumbs";
        public const string cstUI = "PV4SP.CustomUI";
        public const string cstHourCache = "PV4SP.HourCache";
        public const string cstMinCache = "PV4SP.MinCache";
        public const string cstSecCache = "PV4SP.SecCache";
        public const string cstImgSub = "PV4SP.ImgSub";
        private static Dictionary<string, string> defaultValue;
        static PV4SPSettings()
        {
            defaultValue=new Dictionary<string,string>();
            defaultValue.Add(cstMaxItem, "300");
            defaultValue.Add(cst3Layers, true.ToString());
            defaultValue.Add(cstThumbs, true.ToString());
            defaultValue.Add(cstUI, true.ToString());
            defaultValue.Add(cstHourCache, "0");
            defaultValue.Add(cstMinCache, "20");
            defaultValue.Add(cstSecCache, "0");
            defaultValue.Add(cstImgSub, "");
        }
 
        public static string GetValue(SPWeb web, string PropertyName)
        {
            if (!web.Properties.ContainsKey(PropertyName))
            {
                web.Properties.Add(PropertyName, defaultValue[PropertyName]);
                web.AllowUnsafeUpdates = true;
                web.Update();
                web.Properties.Update();
                web.AllowUnsafeUpdates = false;
                web.Update();
            }
            return web.Properties[PropertyName];
        
        }

        public static void SetValue(SPWeb web, string PropertyName, string valeur)
        {
            if (valeur == null)
            {
                web.Properties.Remove(PropertyName);
            }
            else
            {
                if (!web.Properties.ContainsKey(PropertyName))
                {
                    web.Properties.Add(PropertyName, valeur);
                }
                else
                {
                    web.Properties[PropertyName] = valeur;
                }
            }
            web.AllowUnsafeUpdates = true;
            web.Update();
            web.Properties.Update();
            web.AllowUnsafeUpdates = false;
            web.Update();


        }

        public static void ResetDefault(SPWeb web)
        {

            if (!web.Properties.ContainsKey(cstMaxItem))
            {
                web.Properties.Add(cstMaxItem, defaultValue[cstMaxItem]);
                web.Properties.Add(cst3Layers, defaultValue[cst3Layers]);
                web.Properties.Add(cstThumbs, defaultValue[cstThumbs]);
                web.Properties.Add(cstUI, defaultValue[cstUI]);
                web.Properties.Add(cstHourCache, defaultValue[cstHourCache]);
                web.Properties.Add(cstMinCache, defaultValue[cstMinCache]);
                web.Properties.Add(cstSecCache, defaultValue[cstSecCache]);
                web.Properties.Add(cstImgSub, defaultValue[cstImgSub]);
            }
            else
            {
                web.Properties[cstMaxItem] = defaultValue[cstMaxItem];
                web.Properties[cst3Layers] = defaultValue[cst3Layers];
                web.Properties[cstThumbs] = defaultValue[cstThumbs];
                web.Properties[cstUI] = defaultValue[cstUI];
                web.Properties[cstHourCache] = defaultValue[cstHourCache];
                web.Properties[cstMinCache] = defaultValue[cstMinCache];
                web.Properties[cstSecCache] = defaultValue[cstSecCache];
                web.Properties[cstImgSub] = defaultValue[cstImgSub];
            }
            web.AllowUnsafeUpdates = true;
            web.Update();
            web.Properties.Update();
            web.AllowUnsafeUpdates = false;
            web.Update();


        }
        public static void ClearAll(SPWeb web)
        {
            if (web.Properties.ContainsKey(cstMaxItem))
            {
                web.Properties.Remove(cstMaxItem);
            }
            if (web.Properties.ContainsKey(cst3Layers))
            {
                web.Properties.Remove(cst3Layers);
            }
            if (web.Properties.ContainsKey(cstThumbs))
            {
                web.Properties.Remove(cstThumbs);
            }
            if (web.Properties.ContainsKey(cstUI))
            {
                web.Properties.Remove(cstUI);
            }
            if (web.Properties.ContainsKey(cstHourCache))
            {
                web.Properties.Remove(cstHourCache);
            }

            if (web.Properties.ContainsKey(cstMinCache))
            {
                web.Properties.Remove(cstMinCache);
            }
            
            if (web.Properties.ContainsKey(cstSecCache))
            {
                web.Properties.Remove(cstSecCache);
            }
            if (web.Properties.ContainsKey(cstImgSub))
            {
                web.Properties.Remove(cstImgSub);
            }

        }
    }
}
